package com.hyjr.RxTask.demo;

import java.util.Date;
import java.util.Scanner;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

public class NetReceiver implements RxBaseService {
	private static NetReceiver _netReceiver = null;

	public static NetReceiver GetInstance() {
		if (_netReceiver == null) {
			_netReceiver = new NetReceiver();
			// obj = new object();
		}
		return _netReceiver;
	}

	public void run() {
		String nextLine;
		BlockingQueue<RxMsg> queue = RxMsgController.GetInstance().GetRxMsgQueue();

		StringBuilder sb = new StringBuilder();
		Scanner input = new Scanner(System.in);
		int i = 1;
		try {
			while (true) {
				System.out.println("Please input File path-1:");

				// 申明一个临时的字符串变量temp，用来保存 扫描器读取的一行；
				String temp = input.nextLine();
				if ("Esc".equals(temp)) {
					break;
				}
				if ("End".equals(temp)) {
					String si = String.valueOf(i);
					RxMsg msg = new RxMsg();// si, 0x11, 0x01, 0x01, sb);
					msg.setMsgUid(si);
					msg.setTimeStamp(new Date());
//					msg.setSign((byte) RxMsg.REQUEST);
//					msg.setType((byte) RxMsg.HOST);
					msg.setBody(sb.toString());
					
					if (!queue.offer(msg, 2, TimeUnit.SECONDS)) {
						System.out.println("放入数据失败：" + msg);
					} else {
						sb.delete(0, sb.length());
					}
					
				}
				sb.append(temp);
				Thread.sleep(500);
				input = new Scanner(System.in);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
			Thread.currentThread().interrupt();
		} finally {
			System.out.println("退出生产者线程！");
		}
		input.close();
	}
}
